//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// 'Options' dialog, 'Units' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Options.h"
# include "Settings.h"
# include "MapView.h"
# include "PrintUnits.h"
# include "ComboBox.h"
# include "Globals.h"

# define _NL(_s) _s

# pragma const_seg (".text")

static
const CComboBoxData g_udElevation [] = {
	{IDS_UNITS_METERS, unMeters},
	{IDS_UNITS_FEET,   unFeet}
};
const size_t c_cElevations = sizeof (g_udElevation)/sizeof (g_udElevation [0]);

static
const CComboBoxData g_udDistance [] = {
	{IDS_UNITS_METRIC,   unMetric},
	{IDS_UNITS_NAUTICAL, unNautical},
	{IDS_UNITS_STATUTE,  unStatute}
};
const size_t c_cDistances = sizeof (g_udDistance)/sizeof (g_udDistance [0]);

static
const CComboBoxData g_udArea [] = {
	{IDS_UNITS_SQMETERS, unSquareMeters},
	{IDS_UNITS_SQFEET,   unSquareFeet},
	{IDS_UNITS_HECTARES, unHectares},
	{IDS_UNITS_ACRES,    unAcres}
};
const size_t c_cAreas = sizeof (g_udArea)/sizeof (g_udArea [0]);

static
const CComboBoxData g_udAzimuth [] = {
	{IDS_AZIMUTH_DEGREES,     azDegrees},
	{IDS_AZIMUTH_CARDINALITY, azCardinality},
};
const size_t c_cAzimuths = sizeof (g_udAzimuth)/sizeof (g_udAzimuth [0]);

static
const CComboBoxData g_udPositionFormat [] = {
	{IDS_POSFMT_DECIMAL, pfDecimal},
	{IDS_POSFMT_DEGREES, pfDegrees},
	{IDS_POSFMT_MINUTES, pfMinutes},
	{IDS_POSFMT_SECONDS, pfSeconds}
};

const size_t c_cPositionFormats = sizeof (g_udPositionFormat)/sizeof (g_udPositionFormat [0]);

# pragma const_seg ()

INT_PTR CALLBACK _OptionsUnitsProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG: {
			FillComboBox (_hWnd, IDC_ELEVATION_UNITS, g_udElevation,      c_cElevations,      g_ElevationUnit);
			FillComboBox (_hWnd, IDC_DISTANCE_UNITS,  g_udDistance,       c_cDistances,       g_DistanceUnit);
			FillComboBox (_hWnd, IDC_AREA_UNITS,      g_udArea,           c_cAreas,           g_AreaUnit);
			FillComboBox (_hWnd, IDC_AZIMUTH_FORMAT,  g_udAzimuth,        c_cAzimuths,        g_AzimuthFormat);
			FillComboBox (_hWnd, IDC_POSITION_FMT,    g_udPositionFormat, c_cPositionFormats, g_PositionFormat);

			return TRUE;
		}

		case WM_COMMAND: {
			switch (LOWORD (_wParam)) {
				case IDC_ELEVATION_UNITS:
				case IDC_DISTANCE_UNITS:
				case IDC_AREA_UNITS:
				case IDC_AZIMUTH_FORMAT:
				case IDC_POSITION_FMT:
					if (HIWORD (_wParam) == CBN_SELCHANGE)
						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					return TRUE;			
			}
			
			break;
		}
		
		case WM_NOTIFY: {
			const NMHDR * const pInfo = reinterpret_cast<const NMHDR *> (_lParam);
			switch (pInfo->code) {
				case PSN_APPLY: {
					bool bRefreshMap = false;

					// Elevation.
					const HWND hElevation = ::GetDlgItem (_hWnd, IDC_ELEVATION_UNITS);
					size_t cItemSel = ::SendMessage (hElevation, CB_GETCURSEL, 0, 0);
					if (cItemSel != CB_ERR) {
						const int nCode = ::SendMessage (hElevation, CB_GETITEMDATA, cItemSel, 0);
						if (g_ElevationUnit != static_cast<elevation_units_t> (nCode)) {
							g_ElevationUnit = static_cast<elevation_units_t> (nCode);
							SaveSetting (_NL ("OptionElevationUnit"), (DWORD) nCode);

							bRefreshMap = true;
						}
					}

					// Distance.
					const HWND hDistance = ::GetDlgItem (_hWnd, IDC_DISTANCE_UNITS);
					cItemSel = ::SendMessage (hDistance, CB_GETCURSEL, 0, 0);
					if (cItemSel != CB_ERR) {
						const int nCode = ::SendMessage (hDistance, CB_GETITEMDATA, cItemSel, 0);
						if (g_DistanceUnit != static_cast<distance_units_t> (nCode)) {
							g_DistanceUnit = static_cast<distance_units_t> (nCode);
							SaveSetting (_NL ("OptionDistanceUnit"), (DWORD) nCode);

							bRefreshMap = true;
//							SetScaleIndex (g_cCurrentScale);
						}
					}

					// Azimuth.
					const HWND hAzimuth = ::GetDlgItem (_hWnd, IDC_AZIMUTH_FORMAT);
					cItemSel = ::SendMessage (hAzimuth, CB_GETCURSEL, 0, 0);
					if (cItemSel != CB_ERR) {
						const int nCode = ::SendMessage (hAzimuth, CB_GETITEMDATA, cItemSel, 0);
						if (g_AzimuthFormat != static_cast<azimuth_t> (nCode)) {
							g_AzimuthFormat = static_cast<azimuth_t> (nCode);
							SaveSetting (_NL ("OptionAzimuthFormat"), (DWORD) nCode);
						}
					}

					// Area.
					const HWND hArea = ::GetDlgItem (_hWnd, IDC_AREA_UNITS);
					cItemSel = ::SendMessage (hArea, CB_GETCURSEL, 0, 0);
					if (cItemSel != CB_ERR) {
						const int nCode = ::SendMessage (hArea, CB_GETITEMDATA, cItemSel, 0);
						if (g_AreaUnit != static_cast<area_units_t> (nCode)) {
							g_AreaUnit = static_cast<area_units_t> (nCode);
							SaveSetting (_NL ("OptionAreaUnit"), (DWORD) nCode);
						}
					}

					if (bRefreshMap)
						Refresh ();

					// Position format.
					const HWND hPosFmt = ::GetDlgItem (_hWnd, IDC_POSITION_FMT);
					cItemSel = ::SendMessage (hPosFmt, CB_GETCURSEL, 0, 0);
					if (cItemSel != CB_ERR) {
						const int nCode = ::SendMessage (hPosFmt, CB_GETITEMDATA, cItemSel, 0);
						if (g_PositionFormat != static_cast<posfmt_t> (nCode)) {
							g_PositionFormat = static_cast<posfmt_t> (nCode);
							SaveSetting (_NL ("OptionPositionFormat"), (DWORD) nCode);

							Redraw ();
						}
					}
					
					break;
				}
			}

			break;
		}	
	}

	return FALSE;
}
